/*
 * PeriodiqueInterface.java
 *
 * Created on December 16, 2008, 4:02 PM
 */

package biblio;
import javax.swing.*;
import java.util.*;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.tree.DefaultTreeModel;


/**
 *
 * @author  scriptoff
 */
public class VuePeriodiqueGUI extends javax.swing.JDialog implements Observer {
    
    /** Creates new form PeriodiqueInterface */
    public VuePeriodiqueGUI(java.awt.Frame parent, boolean modal ) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(this.getParent());
    }
    
    public void menuPrincipal(Periodique _per) {
        per = _per;
        per.addObserver(this);
        this.afficherTout();
        this.setVisible(true);
    }
     public void update(Observable obs, Object o) {
        // maj de la vue
        this.afficherTout();
    }
    public void elimineObserveur() {
        per.deleteObserver(this);
    }
    private void afficherTout()  {
       text_field_issn.setText(per.issn());
       text_field_nom.setText(per.nom());
       this.afficherParutions();
    }

    private void afficherParutions() {

      DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(per.nom());
      DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);

     parutions_jtree.setModel(treeModel);

     Enumeration mesParutions = per.enumParutions();
      if  (mesParutions.hasMoreElements()) {
          while (mesParutions.hasMoreElements()) {
              Parution paru = (Parution) mesParutions.nextElement();
              DefaultMutableTreeNode x = new DefaultMutableTreeNode(paru.idParution());
              // on affiches tous les articles des parutions
              Set<Article> mesArticles = paru.tesArticles();
              for(Article art:mesArticles) {
                   x.add( new DefaultMutableTreeNode(art.titre()+" - page " + art.page()));
              }
              // x.add( new DefaultMutableTreeNode("filsN° "+ i+"  "+j));
              rootNode.add(x);
          }
      }
     int i=0;
     while(i<=parutions_jtree.getRowCount()) {
        parutions_jtree.expandRow(0);
        i++;
     }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        text_field_issn = new javax.swing.JTextField();
        text_field_nom = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        parutions_jtree = new javax.swing.JTree();
        parutions_periodique_label = new javax.swing.JLabel();
        terminer_button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Vue d'un périodique");

        jLabel1.setText("Numéro ISSN :");

        jLabel2.setText("Nom :");

        text_field_issn.setEditable(false);

        text_field_nom.setEditable(false);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        parutions_jtree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        parutions_jtree.setAutoscrolls(true);
        jScrollPane1.setViewportView(parutions_jtree);

        parutions_periodique_label.setText("Parutions du périodique :");

        terminer_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/flag_red.png"))); // NOI18N
        terminer_button.setText("Terminer");
        terminer_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                terminer_buttonMousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE))
                            .addComponent(parutions_periodique_label))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                            .addComponent(text_field_nom, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                            .addComponent(text_field_issn, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(168, 168, 168)
                        .addComponent(terminer_button)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(text_field_issn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(text_field_nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(parutions_periodique_label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(terminer_button)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void terminer_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_terminer_buttonMousePressed
        // TODO add your handling code here:
        // Vérification la non existence de l'ouvrage / ISBN
        this.dispose();
}//GEN-LAST:event_terminer_buttonMousePressed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VuePeriodiqueGUI dialog = new VuePeriodiqueGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
                dialog.setLocationRelativeTo(null);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree parutions_jtree;
    private javax.swing.JLabel parutions_periodique_label;
    private javax.swing.JButton terminer_button;
    private javax.swing.JTextField text_field_issn;
    private javax.swing.JTextField text_field_nom;
    // End of variables declaration//GEN-END:variables
    private Periodique per ;
}
